﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tzw.Common;

namespace Dafybjy
{
    public partial class ReadCard : Form
    {
        private DafybjyService _service;
        private int _times = AppSettings.CloseTimes;
        public ReadCard()
        {
            InitializeComponent();
        }

        private void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                _service = new DafybjyService();

                //查询病人id
                var hisService = new HisService();
                var brid = hisService.GetPatientIdByCardId(textBox1.Text.Split(':')[0]);

                //查询报告
                var reportInfos = _service.GetReportInfos(brid);

                //保留未打印的报告
                var printReports = reportInfos.sqldata.Where(x => !string.IsNullOrEmpty(x.CheckTime) && x.PrintNum == 0).ToList();

                if (reportInfos.ResultCode == 0 && printReports.Any())
                {
                    var showType = Enums.ReportStatus.Printing;
                    if (reportInfos.sqldata.Count(x => string.IsNullOrEmpty(x.CheckTime)) > 0)
                    {
                        showType = Enums.ReportStatus.PrintingAndHasReport;
                    }

                    //展示提示界面
                    new ShowInfo(showType);

                    //打印报告
                    foreach (var report in printReports)
                    {
                        _service.PrintReport(report);
                    }
                }
                else
                {
                    new ShowInfo(Enums.ReportStatus.NoReport).ShowDialog();
                }
            }
            catch (Exception ex)
            {
                new ShowInfo(Enums.ReportStatus.NoReport).ShowDialog();
                Logger.Default.Error($"his接口异常：{ex.StackTrace},{ex.Message}");
            }

            Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Logger.Default.Info($"程序加载");
            try
            {
                Width = 1024;
                Height = 768;

                MusicPlay.PlayMusic($"{Environment.CurrentDirectory}\\res\\voice\\请扫描手机电子健康卡.mp3");

                _times = AppSettings.CloseTimes;
                timer1.Enabled = true;
            }
            catch (Exception ex)
            {
                Logger.Default.Error($"his接口初始化异常：{ex.StackTrace},{ex.Message}");
                Close();
            }

#if DEBUG
            TopMost = false;
#endif
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            textBox1.Focus();
            //倒计时结束，关闭界面
            if (_times-- < 1)
            {
                Logger.Default.Info($"程序关闭");
                Close();
            }
        }

    }
}
